/*
============================================================================
 Name        : ThreadSocket.h
 Author      : 王斌
 Version     :
 Copyright   : 益盟软件
 Description : CThreadSocket declaration
============================================================================
*/

#ifndef THREADSOCKET_H
#define THREADSOCKET_H

// INCLUDES
#include <e32std.h>
#include <e32base.h>
#include <http\mhttpdatasupplier.h>
#include <http\mhttptransactioncallback.h>
#include <http\mhttpauthenticationcallback.h>
#include <es_sock.h>

/*
* Forward declarations
*/
class RHTTPSession;
class RHTTPTransaction;
class CCommsDatabase;
class CGprsTools;


/*
* MClientObserver
* CThreadSocket passes events and responses body data with this interface.
* An instance of this class must be provided for construction of CClientEngine.
*/
class MClientObserver
{
public:
	
	virtual void ClientEvent(const TDesC& aEventDescription) = 0;

	virtual void ClientBodyReceived(const TDesC8& aBodyData) = 0;

	virtual void ClientUpdateProgress(const TInt& aPercent) = 0;

	virtual void ConnectionOpened() = 0;
};

/*
* Provides simple interface to HTTP Client API.
*/
class CThreadSocket:public CBase,
	public MHTTPTransactionCallback,
	public MHTTPDataSupplier
{
public:
	/*
	* NewL()
	*
	* Create a CClientEngine object.
	*
	* Params:
	*		iObserver:
	*
	* Returns:
	* 		A pointer to the created instance of CClientEngine
	*
	*/
	static CThreadSocket* NewL();

	/*
	* NewLC()
	*
	* Create a CClientEngine object.
	*
	* Params:
	*		iObserver:
	*
	* Returns:
	* 		A pointer to the created instance of CClientEngine
	*
	*/
	static CThreadSocket* NewLC();

	/*
	* ~CClientEngine()
	*
	* Destroy the object
	*
	* Params:
	*		-
	*
	* Returns:
	* 		-
	*
	*/
	~CThreadSocket();


	void CThreadSocket::IssueHTTPGetL(const TDesC8& aUri, MClientObserver* aObserver, TInt nCurSize = -1, TInt nMaxSize = -1);

	/*
	* IssueHTTPPostL()
	*
	* Starts a new HTTP POST transaction.
	*
	* Params:
	*		aUri: URI where to post the data (e.g. http://host.org")
	*		aContentType: Content type of the body, e.g. "text/plain"
	*		aBody: Body data for the transaction.
	*
	* Returns:
	* 		-
	*
	*/
	void IssueHTTPPostL(const TDesC8& aUri,
		const TDesC8& aBody,
		MClientObserver* aObserver,
		const TDesC8& aContentType);

	/*
	* CancelTransaction()
	*
	* Closes currently running transaction and frees resources related to it.
	*
	* Params:
	*		-
	*
	* Returns:
	* 		-
	*
	*/
	void CancelTransaction();

	/*
	* IsRunning()
	*
	* Checks if the transaction is running.
	*
	* Params:
	*		-
	*
	* Returns:
	* 		ETrue, if transaction is currently running.
	*
	*/
	inline TBool IsRunning() { return iRunning; };

private:
	/*
	* ConstructL()
	*
	* Perform the second phase construction of a CClientEngine object.
	*
	* Params:
	*		-
	*
	* Returns:
	* 		-
	*
	*/
	void ConstructL();

	/*
	* CClientEngine()
	*
	* Perform the first phase of two phase construction.
	*
	* Params:
	*		iObserver:
	*
	* Returns:
	* 		-
	*
	*/
	CThreadSocket();

	/*
	* SetHeaderL()
	*
	* Sets header value of an HTTP request.
	*
	* Params:
	*		aHeaders:	Headers of the HTTP request
	*		aHdrField:	Enumerated HTTP header field, e.g. HTTP::EUserAgent
	*		aHdrValue:	New value for header field
	*
	* Returns:
	* 		-
	*
	*/
	void SetHeaderL(RHTTPHeaders aHeaders, TInt aHdrField,
		const TDesC8& aHdrValue);

	/*
	* From MHTTPSessionEventCallback
	*/
private:
	/*
	* MHFRunL()
	*
	* Called by framework to notify about transaction events.
	*
	* Params:
	*		aTransaction: Transaction, where the event occured.
	*		aEvent: Occured event.
	*
	* Returns:
	* 		-
	*
	*/
	void MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent);

	/*
	* MHFRunError()
	*
	* Called by framework when *leave* occurs in handling of transaction event.
	*
	* Params:
	*		aError:			The leave code that occured.
	*		aTransaction:	The transaction that was being processed when leave
	*						occured.
	*		aEvent:			The event that was being processed when leave
	*						occured.
	*
	* Returns:
	* 		KErrNone, if the error was handled. Otherwise the value of aError, or
	*		some other error value. Returning error value causes causes
	*		HTTP-CORE 6 panic.
	*
	*/
	TInt MHFRunError(	TInt aError,
		RHTTPTransaction aTransaction,
		const THTTPEvent& aEvent);

	/*
	* From MHTTPDataSupplier (needed for HTTP POST)
	*/
private:
	/*
	* ReleaseData()
	*
	* Called by framework to allow data supplier to release resources
	* related to previous data part.
	*
	* Params:
	*		-
	*
	* Returns:
	* 		-
	*
	*/
	void ReleaseData();

	/*
	* GetNextDataPart()
	*
	* Called when next data part is needed.
	*
	* Params:
	*		aDataPart: Must be set to point to the next data part.
	*
	* Returns:
	* 		ETrue if the provided data part is the last one. If more data parts
	*		are needed after the provided one, return EFalse.
	*
	*/
	TBool GetNextDataPart(TPtrC8& aDataPart);

	/*
	* Reset()
	*
	* Called by framework to reset the data supplier to its initial state.
	*
	* Params:
	*		-
	*
	* Returns:
	*		KErrNone if successfull.
	*
	*/
	TInt Reset();

	/*
	* OverallDataSize()
	*
	* Called by framework when it needs to know the size of the
	* body data.
	*
	* Params:
	*		-
	*
	* Returns:
	* 		Size of the data, or KErrNotFound (or KErrNotSupported)
	*		if the size of the data is not known.
	*
	*/
	TInt OverallDataSize();

	/*
	* SetupConnectionL()
	* 
	* The method set the internet access point and connection setups.
	* 
	*/
	TBool SetupConnectionL();

	void WriteDataL(const TDesC8& aData);

	void ResetData();

	//
	TBool FindExistingConnection();

public:

//#ifdef __SERIES60_3X__
//	void GetApList(RArray<TIapData>& aApArray);
//#endif

private:
	RHTTPSession			iSession;
	RHTTPTransaction		iTransaction;
	// events to UI.
	HBufC8*					iPostData;	// Data for HTTP POST
	TBool					iRunning;	// ETrue, if transaction running

	TBool 					iConnectionSetupDone;
	
	HBufC8*					iReceivedData;

	TInt					iRemoteFileSize;

	CGprsTools*				m_gprsTools;
public:
	TBool					iBoolWapConfirmed;
	TBool					m_bIOAgain;
	MClientObserver*		iObserver;	// Used for passing body data and
};

#endif // THREADSOCKET_H
